package com.layer8apps;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;

public class DatabaseHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "MovieKeeper";
    private static final String TABLE_MOVIES = "movies";

    private static final String KEY_ID = "id";
    private static final String KEY_MID = "mid";
    private static final String KEY_IMDB = "imdb";
    private static final String KEY_NAME = "name";
    private static final String KEY_TAG = "tag";
    private static final String KEY_IMGURL = "imageURL";
    private static final String KEY_IMAGE = "image";
    private static final String KEY_UPC = "UPC";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
        onCreate(sqLiteDatabase);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_MOVIES_TABLE = "CREATE TABLE " + TABLE_MOVIES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_MID + " TEXT," + KEY_IMDB + " TEXT," + KEY_NAME + " TEXT," + KEY_TAG + " TEXT," + KEY_IMGURL + " TEXT," + KEY_UPC + " TEXT," + KEY_IMAGE + " TEXT)";
        sqLiteDatabase.execSQL(CREATE_MOVIES_TABLE);
    }

    public void dropTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
    }

    public void addProduct(Product product) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(KEY_MID, product.getMid());
        cv.put(KEY_IMDB, product.getImdb());
        cv.put(KEY_NAME, product.getName());
        cv.put(KEY_TAG, product.getTag());
        cv.put(KEY_IMGURL, product.getImageURL());
        cv.put(KEY_UPC, product.getUpc());
        cv.put(KEY_IMAGE, product.getImage());
        db.insert(TABLE_MOVIES, null, cv);
        db.close();
    }

    public Product getProduct(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_MOVIES, new String[] {KEY_ID, KEY_MID, KEY_IMDB, KEY_NAME, KEY_TAG, KEY_IMGURL, KEY_IMAGE, KEY_UPC}, KEY_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            Product product = new Product(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
            return product;
        }
        return null;
    }

    public ArrayList<Product> getAllProducts() {
        ArrayList<Product> productList = new ArrayList<Product>();
        String selectQuery = "SELECT * FROM " + TABLE_MOVIES;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {

                Product product = new Product();
                product.setId(Integer.parseInt(cursor.getString(0)));
                product.setMid(cursor.getString(1));
                product.setImdb(cursor.getString(2));
                product.setName(cursor.getString(3));
                product.setTag(cursor.getString(4));
                product.setImageURL(cursor.getString(5));
                product.setImage(cursor.getString(6));
                product.setUpc(cursor.getString(7));
                productList.add(product);
            } while (cursor.moveToNext());
            return productList;
        }
        return null;
    }

    public int getProductCount() {
        String countQuery = "SELECT * FROM " + TABLE_MOVIES;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getCount();
    }

    public int updateProduct(Product product) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(KEY_MID, product.getMid());
        cv.put(KEY_IMDB, product.getImdb());
        cv.put(KEY_NAME, product.getName());
        cv.put(KEY_TAG, product.getTag());
        cv.put(KEY_IMGURL, product.getImageURL());
        cv.put(KEY_IMAGE, product.getImage());
        cv.put(KEY_UPC, product.getUpc());
        return db.update(TABLE_MOVIES, cv, KEY_ID + " = ?", new String[] {String.valueOf(product.getId())});
    }

    public void deleteProduct(Product product) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_MOVIES, KEY_ID + " = ?", new String[] {String.valueOf(product.getId())});
        db.close();
    }
}
